# Author: mosra <mosra@centrum.cz>
pkgname=magnum
pkgver=dev
pkgrel=1
pkgdesc="C++11/C++14 graphics middleware for games and data visualization"
arch=('i686' 'x86_64')
url="https://magnum.graphics"
license=('MIT')
depends=('corrade' 'openal' 'sdl2' 'glfw' 'vulkan-icd-loader')
makedepends=('cmake' 'ninja')
checkdepends=('swiftshader')
options=('!strip')
provides=('magnum-git')

_rootdir=$startdir/../../

build() {
    mkdir -p "$_rootdir/build"
    cd "$_rootdir/build"

    # Disable optimization (saves A LOT of compilation time)
    newcxxflags=$(echo $CXXFLAGS | sed s/-O.//g | sed s/-D_FORTIFY_SOURCE=.//g)
    export CXXFLAGS="$newcxxflags"

    cmake .. \
        -DCMAKE_BUILD_TYPE=Debug \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DWITH_AUDIO=ON \
        -DWITH_VK=ON \
        -DWITH_GLFWAPPLICATION=ON \
        -DWITH_GLXAPPLICATION=ON \
        -DWITH_SDL2APPLICATION=ON \
        -DWITH_XEGLAPPLICATION=ON \
        -DWITH_WINDOWLESSGLXAPPLICATION=ON \
        -DWITH_WINDOWLESSEGLAPPLICATION=ON \
        -DWITH_EGLCONTEXT=ON \
        -DWITH_GLXCONTEXT=ON \
        -DWITH_OPENGLTESTER=ON \
        -DWITH_ANYAUDIOIMPORTER=ON \
        -DWITH_ANYIMAGECONVERTER=ON \
        -DWITH_ANYIMAGEIMPORTER=ON \
        -DWITH_ANYSCENECONVERTER=ON \
        -DWITH_ANYSCENEIMPORTER=ON \
        -DWITH_ANYSHADERCONVERTER=ON \
        -DWITH_MAGNUMFONT=ON \
        -DWITH_MAGNUMFONTCONVERTER=ON \
        -DWITH_OBJIMPORTER=ON \
        -DWITH_TGAIMAGECONVERTER=ON \
        -DWITH_TGAIMPORTER=ON \
        -DWITH_WAVAUDIOIMPORTER=ON \
        -DWITH_DISTANCEFIELDCONVERTER=ON \
        -DWITH_FONTCONVERTER=ON \
        -DWITH_IMAGECONVERTER=ON \
        -DWITH_SCENECONVERTER=ON \
        -DWITH_SHADERCONVERTER=ON \
        -DWITH_GL_INFO=ON \
        -DWITH_VK_INFO=ON \
        -DWITH_AL_INFO=ON \
        -DBUILD_TESTS=ON \
        -DBUILD_GL_TESTS=ON \
        -DBUILD_VK_TESTS=ON \
        -G Ninja
    ninja
}

check() {
    cd "$_rootdir/build"
    CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5
    MAGNUM_DISABLE_EXTENSIONS="GL_ARB_invalidate_subdata GL_ARB_multi_bind GL_ARB_robustness GL_ARB_separate_shader_objects GL_ARB_texture_storage GL_ARB_texture_storage_multisample GL_ARB_shading_language_420pack GL_ARB_explicit_uniform_location GL_ARB_explicit_attrib_location GL_ARB_texture_filter_anisotropic" CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5 -R GLTest
    MAGNUM_DISABLE_EXTENSIONS="GL_ARB_direct_state_access" CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5 -R GLTest
    MAGNUM_DISABLE_EXTENSIONS="GL_ARB_get_texture_sub_image" CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5 -R GLTest
    MAGNUM_DISABLE_EXTENSIONS="GL_ARB_vertex_array_object" CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5 -R GLTest
    MAGNUM_DISABLE_EXTENSIONS="GL_KHR_debug" CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5 -R GLTest

    # Run all Vulkan tests with SwiftShader as well
    # Keep in sync with PKGBUILD-coverage, PKGBUILD-release and
    # package/ci/unix-desktop-vulkan.sh
    MAGNUM_DEVICE=cpu CORRADE_TEST_SKIP_BENCHMARKS=ON CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5 -R VkTest
    for device in "" cpu; do
        MAGNUM_DEVICE=$device MAGNUM_VULKAN_VERSION=1.0 CORRADE_TEST_SKIP_BENCHMARKS=ON CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5 -R VkTest
        MAGNUM_DEVICE=$device MAGNUM_DISABLE_EXTENSIONS="VK_KHR_get_physical_device_properties2 VK_KHR_get_memory_requirements2 VK_KHR_bind_memory2 VK_KHR_create_renderpass2 VK_KHR_copy_commands2 VK_KHR_maintenance1 VK_KHR_multiview VK_KHR_maintenance2" MAGNUM_VULKAN_VERSION=1.0 CORRADE_TEST_SKIP_BENCHMARKS=ON CORRADE_TEST_COLOR=ON ctest --output-on-failure -j5 -R VkTest
    done
}

package() {
    cd "$_rootdir/build"
    DESTDIR="$pkgdir/" ninja install
}
